package com.aisile.admin.controller.v1;

import cn.hutool.crypto.digest.BCrypt;
import cn.hutool.crypto.digest.DigestUtil;
import com.aisile.api.admin.AdChannelControllerApi;
import com.aisile.admin.service.AdChannelService;
import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.model.admin.dtos.ChannelDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.admin.pojos.AdUser;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.aisile.utils.threadlocal.AdminThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Auther:yry
 * @Date:2024/7/9 0009
 * @VERSON:1.0
 */
@RestController
@RequestMapping("/api/v1/adchannel")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;

    @Autowired
    HttpServletRequest request;


    @PostMapping("/list")
    @Override
    public ResponseResult findByPage(@RequestBody ChannelDto dto) {
//        String token = request.getHeader("token");
//        Claims claims = AppJwtUtil.getClaimsBody(token);
//        if (claims == null || claims.get("id") == null) {
//            CustomExceptionThrows.catchAdmin(AdminHttpCodeEnum.NEED_LOGIN);
//        }

        AdUser adUser =  AdminThreadLocalUtils.getUser();
        if(adUser==null || adUser.getId()==null){
            CustomExceptionThrows.catchAdmin(AdminHttpCodeEnum.NEED_LOGIN);
        }
        return adChannelService.findByPage(dto);
    }

    @PostMapping("/save")
    @Override
    public ResponseResult saveChannel(@RequestBody AdChannel adChannel) {
        return adChannelService.saveChannel(adChannel);
    }

    @GetMapping("/one/{id}")
    @Override
    public ResponseResult getOne(@PathVariable("id") Integer id) {
        return null;
    }

    @PutMapping("/update/{id}")
    @Override
    public ResponseResult updateChannel(@PathVariable("id") Integer id, @RequestBody AdChannel adChannel) {
        return null;
    }


    // @RequestPart("file")   @RequestParam("file")  有什么区别？
    // 682bb89ee6e48bf6220ce66307e4e1be
    // 6fc376b1aaf52cbde1d216ccb9c42397
    // 6fc376b1aaf52cbde1d216ccb9c42397
    // 6fc376b1aaf52cbde1d216ccb9c42397
    // 084cd042a7e7d1c1295e62591210fcc5
    @PostMapping
    public String testUpLoadFile(@RequestPart("file") MultipartFile file) throws IOException {

        // $2a$10$bdnD41q9ETtsl/QsSZ5VjOEgb5INkFb//ySRsd67I7CYrSuUj52Cm
        // $2a$10$KN5GBG8tVSse2gGUywvSTudDhVSlz.8vX/Nd9N.k4O6dP11YZaEiS
        // $2a$10$kroyz/gfE/amyq.4mPxLFOTL7AkCcDWDoRSkKVbKeUl5Teo0Xkc9u
        String str = DigestUtil.bcrypt("123456");
        System.out.println(DigestUtil.bcryptCheck("123456", "$2a$10$kroyz/gfE/amyq.4mPxLFOTL7AkCcDWDoRSkKVbKeUl5Teo0Xkc9u"));
        System.out.println(DigestUtil.bcryptCheck("123456", "$2a$10$KN5GBG8tVSse2gGUywvSTudDhVSlz.8vX/Nd9N.k4O6dP11YZaEiS"));
        System.out.println(DigestUtil.bcryptCheck("123456", "$2a$10$bdnD41q9ETtsl/QsSZ5VjOEgb5INkFb//ySRsd67I7CYrSuUj52Cm"));

        BCrypt.hashpw("123456", BCrypt.gensalt());
        //BCrypt.checkpw()


        //String md5Str =  MD5.create().digestHex(file.getInputStream());
        return str;
    }

}
